<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="700px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        style="margin-left: 55px"
      >
        <el-col :span="12">
          <el-form-item label="物料代码" prop="itmWldm">
            <el-input
              v-model="queryParams.itmWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物品品名" prop="itmPmgg">
            <el-input
              v-model="queryParams.itmPmgg"
              placeholder="请输入物品品名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态标志" prop="itmZtbz">
            <el-select
              v-model="queryParams.itmZtbz"
              placeholder="请选择状态标志"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in itmZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户编码" prop="itmCustwldm">
            <el-input
              v-model="queryParams.itmCustwldm"
              placeholder="请输入客户编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearchs"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="singe"
          @click="handleAudit"
          v-hasPermi="['project:itmAudit:check']"
        >
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:itmAudit:back']"
        >
          撤回
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:itmAudit:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid rgb(0, 126, 255);
              background-color: rgb(0, 126, 255);
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">未审核</span>
        </div>
      </el-col>
      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid #bb0331;
              background-color: #bb0331;
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">已审核</span>
        </div>
      </el-col>
      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid rgb(15, 152, 86);
              background-color: rgb(15, 152, 86);
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">已批准</span>
        </div>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      border
      size="mini"
      :cell-style="iCellStyle"
      :header-cell-style="iHeaderCellStyle"
      :data="itmAuditList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="物料代码"
        width="130"
        align="center"
        prop="itmWldm"
      />
      <el-table-column
        label="物品品名"
        width="140"
        show-overflow-tooltip
        align="center"
        prop="itmWlpm"
      />
      <el-table-column
        label="物料规格"
        show-overflow-tooltip
        width="350"
        prop="itmWlgg"
      />
      <el-table-column label="单位" align="center" prop="itmDw">
        <template #default="scope">
          <dict-tag :options="itmDwOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="客户编码"
        show-overflow-tooltip
        width="130"
        align="center"
        prop="itmCustwldm"
      />
      <el-table-column
        label="图纸编号"
        show-overflow-tooltip
        width="130"
        align="center"
        prop="itmTzbh"
      />
      <el-table-column
        label="报价周期"
        show-overflow-tooltip
        align="center"
        prop="itmTedzq"
      />
      <el-table-column
        label="建立人员"
        show-overflow-tooltip
        align="center"
        prop="createBy"
      />
      <el-table-column
        label="建立日期"
        align="center"
        prop="createTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="审核人员" align="center" prop="itmShry" />
      <el-table-column
        label="审核日期"
        align="center"
        prop="itmShrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.itmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="批准人员" align="center" prop="itmPzry" />
      <el-table-column
        label="批准日期"
        align="center"
        prop="itmPzrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.itmPzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  listItmAudit,
  getItmAudit,
  withdrawItmAudit,
  audItmAudit,
  updateItmAudit,
  exportItmAudit,
} from "@/api/project/itmAudit";
import { getAllCgList } from "@/api/project/mstr";

export default {
  name: "ItmAudit",
  data() {
    return {
      tableH: 1,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      wldms: [],
      // 非单个禁用
      single: true,
      singe: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产物料报价周期审核表格数据
      itmAuditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openSearch: false,
      // 单位字典
      itmDwOptions: [],
      // 状态标志字典
      itmZtbzOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itmWldm: null,
        itmPmgg: null,
        itmCustwldm: null,
        itmZtbz: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_unit").then((response) => {
      this.itmDwOptions = response.data;
    });
    this.getDicts("sys_audit").then((response) => {
      this.itmZtbzOptions = response.data;
    });
  },
  methods: {
    /** 查询生产物料报价周期审核列表 */
    getList() {
      this.loading = true;
      this.tableH = (document.body.clientHeight - 160) / 2;
      this.queryParams.pageSize = this.tableH > 350 ? 25 : 15;
      listItmAudit(this.queryParams).then((response) => {
        this.itmAuditList = response.rows;
        this.total = response.total;

        this.loading = false;
      });
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //未审核
      if (row.itmZtbz === 1) {
        return "padding:0px 0;color:rgb(0, 126, 255)";
      }
      //已审核color:#bb0331
      else if (row.itmZtbz === 2) {
        return "padding:0px 0;color:#bb0331";
      }
      //已批准
      else if (row.itmZtbz === 4) {
        return "padding:0px 0;color:rgb(60,179,113)";
      }
    },
    iHeaderCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      return "padding:0px;text-align:center";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    // 表单重置
    reset() {
      this.form = {
        itmId: null,
        itmWldm: null,
        itmWlpm: null,
        itmWlgg: null,
        itmPmgg: null,
        itmCzdm: null,
        itmYsdm: null,
        itmQtinf: null,
        itmMz: null,
        itmJz: null,
        itmSkzl: null,
        itmDw: null,
        itmCustwldm: null,
        itmTzbh: null,
        itmCgsx: null,
        itmMinbz: null,
        itmCwdm: null,
        itmCkdm: null,
        itmKwdm: null,
        itmJzdm: null,
        itmCcms: null,
        itmZtbz: 0,
        itmEdzq: null,
        itmTedzq: null,
        itmShry: null,
        itmShrq: null,
        itmPzry: null,
        itmPzrq: null,
        itmJccc1: null,
        itmJccc2: null,
        itmJccc3: null,
        itmCj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.itmId);
      this.wldms = selection.map((item) => item.itmWldm);
      this.single = selection.length !== 1;
      selection.map((item) => {
        this.singe = item.itmZtbz == 1 ? false : true;
      });
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itmIds = row.itmId || this.ids;
      const itmWldms = row.itmWldm || this.wldms;
      this.$confirm(
        '是否确认撤回物料代码为"' + itmWldms + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return withdrawItmAudit(itmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("撤回成功");
        })
        .catch(() => {});
    },
    handleSearchs() {
      this.reset();
      this.title = "查询bom资料维护";
      this.openSearch = true;
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      const itmIds = row.itmId || this.ids;
      const itmWldms = row.itmWldm || this.wldms;
      this.$confirm(
        '是否确认审核物料代码为"' + itmWldms + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return audItmAudit(itmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("审核成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有生产物料报价周期审核数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportItmAudit(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
